package nml.webmail;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ymail_cn extends WebMail {

	public void send(String uid, String password, String to, String subject, String content) throws Exception {

		try {
			String url = "http://www.ymail.cn/ServiceLogin.jsp";
			dr.get(url);

			dr.findElement(By.name("username")).sendKeys(uid);
			dr.findElement(By.name("password")).sendKeys(password);
			dr.findElement(By.name("maillang")).sendKeys("简约版");

			dr.findElement(By.name("loginform")).submit();

			
			
			
			waitWebElement(By.name("folder"));
			dr.switchTo().frame("folder");
			

			waitWebElement(By.linkText("写信")).click();

			
			
			
			dr.switchTo().defaultContent().switchTo().frame("foldmain");

			waitWebElement(By.className("gTbrBtm"));
			
			loadJQuery();
			String js=loadJS();
			
			content=StringUtils.remove(content, '\r');
			content=StringUtils.remove(content, '\n');
			content=StringEscapeUtils.escapeJavaScript(content);
			
			js = StringUtils.replace(js, "$content", content);
			js = StringUtils.replace(js, "$subject", subject);
			js = StringUtils.replace(js, "$to", to);

			executeScript(js);
			 

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			dr.quit();
		}
	}

	public static void main(String[] args) {
		try {
			String uid = "qq331798654";
			String password = "qq3131798654";
			String to = "o21x@qq.com";
			String subject = "nice chek in";
			String content = FileUtils.readFileToString(new File("a.txt"));

			new ymail_cn().send(uid, password, to, subject, content);
 		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
